package com.xiaochong.tir.manager.controller;

import com.xiaochong.service.common.config.oss.OSSTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.manager.config.oss.OSSToolManage;
import com.xiaochong.tir.manager.util.ReturnResult;

/**
 * 文件上传至oss
 * 
 * @author 杨松松
 *
 */
@Controller
@RequestMapping("/manage/upload")
@Api("文件上传统一接口")
public class UploadFileController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private OSSToolManage oSSToolManage;

	@Autowired
	private OSSTool ossTool;

	/**
	 * @Title: uploadFile
	 * @Description: TODO(上传图片)
	 * @param file	上传文件
	 * @return
	 */
	@ApiOperation(value = "上传文件统一接口",notes = "除富文本编辑器之外的所有文件上传都用此接口")
	@PostMapping("/uploadFile")
	@ResponseBody
	public Object uploadFile(@RequestParam(value = "file", required = false) MultipartFile file) {
		String prefix = "png";
		String imgUrl=null;
		String fileName ="";
		Map<String,Object> map = new HashMap<>();
		if (file.getSize() > 2000000) {
			return new ReturnResult<Object>(-1, "文件太大,请重新选择文件！", null, 0);
		}
		prefix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
		fileName = UUID.randomUUID().toString().replace("-", "") + "." + prefix;
		map.put("key",fileName);
		try {
			boolean flag = ossTool.upload(file.getInputStream(),fileName);
			if(flag){
				imgUrl=ossTool.getOSSUrl( fileName );
				map.put("imgUrl",imgUrl);
				return new com.xiaochong.tir.common.data.vo.ReturnResult<Object>(0, "上传成功！", map, 0);
			}else{
				return new ReturnResult<Object>(-1, "文件上传失败！", null, 0);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return new ReturnResult<Object>(-1, "文件上传失败！", null, 0);
		}
	}

	/**
	 * 上传图片
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
	@ResponseBody
	public Object uploadFiles(@RequestParam(value = "file", required = false) MultipartFile file,
			@RequestParam(value = "dir", required = false) String dir) {
		try {
			if (file.getSize() > 2000000) {
				return new ReturnResult<Object>(-1, "文件太大,请重新选择文件！", null, 0);
			}
			if (StringUtils.isEmpty(dir)) {
				dir = "others";
			}
			String fileName = file.getOriginalFilename();
			/*
			 * StringBuffer imgUrl = new StringBuffer("manage/" + dir + "/");
			 * imgUrl.append(fileName);
			 */
			String imgUrl = oSSToolManage.uploadImage(file.getInputStream(), "manage/" + dir, fileName);
			System.out.println(imgUrl);
			LogTrace.info("上传文件路径","imgurl", imgUrl);
			return new ReturnResult<Object>(0, "上传成功！", imgUrl, 0);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("上传文件失败：" + e);
			LogTrace.error(getClass().getSimpleName()+"/uploadFile", e.getMessage(), e);
			return new ReturnResult<Object>(-1, "文件上传失败！", null, 0);
		}
	}
}
